<?php

/**
 * When a page is called, the page controller is run before any output is made. 
 * The controller's job is to transform the HTTP request into business objects, 
 * then call the approriate logic and prepare the objects used to display the response.
 * 
 * The page logic performs the following steps:
 * 1. The cmd request parameter is evaluated.
 * 2. Based on the action other request parameters are evaluated.
 * 3. Value Objects (or a form object for more complex tasks) are created from the parameters.
 * 4. The objects are validated and the result is stored in an error array.
 * 5. The business logic is called with the Value Objects.
 * 6. Return status (error codes) from the business logic is evaluated.
 * 7. A redirect to another page is executed if necessary.
 * 8. All data needed to display the page is collected and made available to the page as variables of the controller. Do not use global variables.
 *
 * * @author dintech
 * 
 */

require_once('include/header.php');
require_once('vo/NoticiasVO.php');
require_once('search/NoticiasSearchCriteria.php');
require_once('logic/NoticiasLogic.php');
//require_once('logic/TagsLogic.php');
require_once('plugin/CommonPlugin.php');
require('conf/configuration.php');
require_once('logic/ImagenesLogic.php');
require_once('search/ImagenesSearchCriteria.php');


$noticiasLogic = new NoticiasLogic($_POST);
$imagenesLogic = new ImagenesLogic($_POST);

// Ordenamiento
if($_GET[order] == "asc")
	$order= "desc";
else if($_GET[order] == "desc")
	$order= "asc";
else
	$order= "desc";
if (!isset($_GET["orderby"]))
	$orderby = "Fecha";
else
	$orderby = $_GET["orderby"];
$smarty->assign("orderby", $orderby);
$smarty->assign("order", $order);

// obtener Noticia por id (si aplica)
if (isset($_GET['id']) && ($_GET['id'] != '')) {
	$id = $_GET['id'];
	$noticia = $noticiasLogic->get($id);
	if ($noticia != null)
		$smarty->assign('noticia', $noticia);
} 

// obtener action
$action = $_GET['action'];
if ($action == null){
	$action = $_POST['action'];
}

	
// Seteo el searchcriteria para mostrar la tabla
$noticiasSearchCriteria = new NoticiasSearchCriteria();
$noticiasSearchCriteria->setTitulo($_GET['titulo']);
$noticiasSearchCriteria->setFechaFrom($_GET['desde']);
$noticiasSearchCriteria->setFechaTo($_GET['hasta']);

$noticiasSearchCriteria->setOrderby($orderby);
$noticiasSearchCriteria->setOrder($order);
$noticiasSearchCriteria->setInicio(1);
$noticiasSearchCriteria->setFin(null);


switch ($action){
	case "save":
		$result = $noticiasLogic->save($_POST);
		if ($result == "error")
			$smarty->assign("error", "Ha ocurrido un error al guardar los cambios en la Noticia");
		else
			$smarty->assign("message", "La noticia ha sido actualizada");
		break;
	case "add":
		require('conf/enum.php');
		$smarty->assign('categoriasList', $categoriasList);
		$smarty->assign("detailPage","noticiasForm.tpl");
		$smarty->display("admin_generic.tpl");
		exit;break;
	case "update":	
		require('conf/enum.php');
		$imageSearchCriteria = new ImagenesSearchCriteria();
		$imageSearchCriteria->setIditem($id);
		$imageSearchCriteria->setTable('contenido');
		$images = $imagenesLogic->find($imageSearchCriteria);
		$smarty->assign('images',$images);
		$smarty->assign('categoriasList', $categoriasList);
		$smarty->assign("detailPage","noticiasForm.tpl");
		$smarty->display("admin_generic.tpl");
		exit;break;
	case "detail":
		// obtengo todas las imagenes
		$imageSearchCriteria = new ImagenesSearchCriteria();
		$imageSearchCriteria->setIditem($id);
		$imageSearchCriteria->setTable('noticias');
		$images = $imagenesLogic->find($imageSearchCriteria);
		$smarty->assign('images',$images);
		$smarty->assign("detailPage","noticiasDetails.tpl");
		$smarty->display("admin_generic.tpl");
		exit;break;
	case "delete":
		$result = $noticiasLogic->delete($_POST['id']);
		if ($result == 'error'){$smarty->assign('error', "Error al eliminar la noticia");}
		break;
	case "activar":
		$result = $noticiasLogic->activar($_GET['id']);
		if ($result == 'error'){$smarty->assign('error', "Error al activar la noticia");}
		break;
	case "desactivar":
		$result = $noticiasLogic->desactivar($_GET['id']);
		if ($result == 'error'){$smarty->assign('error', "Error al activar la noticia");}
		break;
}


$cantidad = $noticiasLogic->count($noticiasSearchCriteria);

// Paginador
if (isset($_GET["entradas"]))
	$totalItemsPerPage=$_GET["entradas"];
$totalPages = ceil($cantidad / $totalItemsPerPage);
$smarty->assign('totalPages', $totalPages);
$smarty->assign('totalItemsPerPage', $totalItemsPerPage);
$page = $_GET[page];
if (!$page) {
	$start = 0;
	$page = 1;
}
else
	$start = ($page - 1) * $totalItemsPerPage;
$smarty->assign('page', $page);

$noticiasSearchCriteria->setInicio($start);	
$noticiasSearchCriteria->setFin($totalItemsPerPage);
$noticias = $noticiasLogic->find($noticiasSearchCriteria);

$smarty->assign('noticias', $noticias);
$smarty->assign('allParams', CommonPlugin::getParamsAsString($_GET));
$smarty->assign('cantidad', $cantidad);
$smarty->assign('titulo', $_GET['titulo']);
$smarty->assign('fechaFrom', $_GET['desde']);
$smarty->assign('fechaTo', $_GET['hasta']);

$smarty->assign("detailPage", "noticias.tpl");
$smarty->display("admin_generic.tpl");


?>